package com.splant.android.smart.SystemDefinedLevel.GenericControls.NumberView;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.View;

import com.splant.android.smart.SystemDefinedLevel.Utils.System_DataType_Util;

/**
 * Created by Administrator on 2016/10/3.
 */
public class NumberView extends View {

    private boolean[] visilitys = {true, true, true, true, true, true, true};//是否显示对应笔画
    private boolean[] drawBooleans = new boolean[7];
    private int[] bitPaintIndexs = {0, 1, 2, 3, 4, 5, 6};//上 中 下 左上 右上 左下 右下

    public NumberView(Context context) {

        super(context);
        init();
    }

    public NumberView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public NumberView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public NumberView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private Paint paint;

    private void init() {
        paint = new Paint();
        paint.setColor(drawPaintColor);
        paint.setStrokeWidth(2);
        paint.setStyle(Paint.Style.STROKE);
    }

    public void setVisilitys(boolean[] visilitys) {
        if (visilitys != null && visilitys.length == this.visilitys.length) {
            for (int i = 0; i < visilitys.length; i++) {
                this.visilitys[i] = visilitys[i];
            }
        }
    }

    public void setBitPaintIndexs(int[] bitPaintIndexs) {
        if (bitPaintIndexs != null && bitPaintIndexs.length == this.bitPaintIndexs.length) {
            for (int i = 0; i < bitPaintIndexs.length; i++) {
                this.bitPaintIndexs[i] = bitPaintIndexs[i];
            }
        }
    }

    public void setValue(byte value) {
        String bits = System_DataType_Util.byteToBit(value).substring(1);
        char[] bitChars = bits.toCharArray();
        for (int i = 0; i <bitChars.length; i++) {
            int index=bitPaintIndexs[i];
            char c = bitChars[bitChars.length-index-1];
            if (c == '0')
                drawBooleans[i] = false;
            else
                drawBooleans[i] = true;
        }
        invalidate();
    }

    private final int paddingSize = 3;
    private final int drawPaintColor = Color.BLACK;

    private final int niceWidth = 120;
    private final int niceHeight = 200;

    private void drawTop(Canvas canvas) {

        if (visilitys[0] == false)
            return;

        int w = getWidth();
        int h = getHeight();

        final float factorX = w * 1.0f / niceWidth;
        final float factorY = h * 1.0f / niceHeight;

        final int topPointMarginLeftRight = (int) (12 * factorX);
        final int middlePointMarginLeftRight = (int) (6 * factorX);
        final int bottomPointMarginLeftRight = (int) (14 * factorX);
        final int topMiddlePointYDis = (int) (6 * factorY);
        final int middleBottomPointYDis = (int) (8 * factorY);

        Point topLeft = new Point(paddingSize + topPointMarginLeftRight, paddingSize);
        Point topRight = new Point(w - topLeft.x, topLeft.y);
        Point midLeft = new Point(paddingSize + middlePointMarginLeftRight, topLeft.y + topMiddlePointYDis);
        Point midRight = new Point(w - midLeft.x, midLeft.y);
        Point bottomLeft = new Point(paddingSize + bottomPointMarginLeftRight, midLeft.y + middleBottomPointYDis);
        Point bottomRight = new Point(w - bottomLeft.x, bottomLeft.y);

        Path p = new Path();
        p.moveTo(topLeft.x, topLeft.y);
        p.lineTo(topRight.x, topRight.y);
        p.lineTo(midRight.x, midRight.y);
        p.lineTo(bottomRight.x, bottomRight.y);
        p.lineTo(bottomLeft.x, bottomLeft.y);
        p.lineTo(midLeft.x, midLeft.y);
        p.close();

        if (drawBooleans[0])
            paint.setStyle(Paint.Style.FILL);
        else
            paint.setStyle(Paint.Style.STROKE);

        canvas.drawPath(p, paint);
    }

    private void drawMiddle(Canvas canvas) {
        if (visilitys[1] == false)
            return;

        int w = getWidth();
        int h = getHeight();
        int halfHeight = h / 2;
        final float factorX = w * 1.0f / niceWidth;
        final float factorY = h * 1.0f / niceHeight;

        final int topPointMarginLeftRight = (int) (12 * factorX);
        final int middlePointMarginLeftRight = (int) (6 * factorX);
        final int bottomPointMarginLeftRight = (int) (12 * factorX);
        final int topMiddlePointYDis = (int) (7 * factorY);
        final int middleBottomPointYDis = (int) (7 * factorY);

        Point topLeft = new Point(paddingSize + topPointMarginLeftRight, halfHeight - topMiddlePointYDis);
        Point topRight = new Point(w - topLeft.x, topLeft.y);
        Point midLeft = new Point(paddingSize + middlePointMarginLeftRight, halfHeight);
        Point midRight = new Point(w - midLeft.x, midLeft.y);
        Point bottomLeft = new Point(paddingSize + bottomPointMarginLeftRight, halfHeight + middleBottomPointYDis);
        Point bottomRight = new Point(w - bottomLeft.x, bottomLeft.y);

        Path p = new Path();
        p.moveTo(topLeft.x, topLeft.y);
        p.lineTo(topRight.x, topRight.y);
        p.lineTo(midRight.x, midRight.y);
        p.lineTo(bottomRight.x, bottomRight.y);
        p.lineTo(bottomLeft.x, bottomLeft.y);
        p.lineTo(midLeft.x, midLeft.y);
        p.close();

        if (drawBooleans[1])
            paint.setStyle(Paint.Style.FILL);
        else
            paint.setStyle(Paint.Style.STROKE);

        canvas.drawPath(p, paint);
    }

    private void drawBottom(Canvas canvas) {
        if (visilitys[2] == false)
            return;

        int w = getWidth();
        int h = getHeight();

        final float factorX = w * 1.0f / niceWidth;
        final float factorY = h * 1.0f / niceHeight;

        final int topPointMarginLeftRight = (int) (14 * factorX);
        final int middlePointMarginLeftRight = (int) (6 * factorX);
        final int bottomPointMarginLeftRight = (int) (12 * factorX);
        final int topMiddlePointYDis = (int) (8 * factorY);
        final int middleBottomPointYDis = (int) (6 * factorY);


        int bottomY = h - paddingSize;

        Point topLeft = new Point(paddingSize + topPointMarginLeftRight, bottomY - topMiddlePointYDis - middleBottomPointYDis);
        Point topRight = new Point(w - topLeft.x, topLeft.y);
        Point midLeft = new Point(paddingSize + middlePointMarginLeftRight, bottomY - middleBottomPointYDis);
        Point midRight = new Point(w - midLeft.x, midLeft.y);
        Point bottomLeft = new Point(paddingSize + bottomPointMarginLeftRight, bottomY);
        Point bottomRight = new Point(w - bottomLeft.x, bottomLeft.y);

        Path p = new Path();
        p.moveTo(topLeft.x, topLeft.y);
        p.lineTo(topRight.x, topRight.y);
        p.lineTo(midRight.x, midRight.y);
        p.lineTo(bottomRight.x, bottomRight.y);
        p.lineTo(bottomLeft.x, bottomLeft.y);
        p.lineTo(midLeft.x, midLeft.y);
        p.close();

        if (drawBooleans[2])
            paint.setStyle(Paint.Style.FILL);
        else
            paint.setStyle(Paint.Style.STROKE);

        canvas.drawPath(p, paint);
    }

    private void drawTopLeft(Canvas canvas) {
        if (visilitys[3] == false)
            return;

        int w = getWidth();
        int h = getHeight();
        int halfHeight = h / 2;
        final float factorX = w * 1.0f / niceWidth;
        final float factorY = h * 1.0f / niceHeight;

        final int leftRightToMiddlePointXDis = (int) (6 * factorX);
        final int middleToLeftRightPointXDis = (int) (8 * factorX);
        final int marginTop1 = (int) (6 * factorY);
        final int marginTop2 = (int) (12 * factorY);
        final int marginTop3 = (int) (14 * factorY);

        Point firstPoint = new Point(paddingSize + leftRightToMiddlePointXDis, paddingSize + marginTop1);
        Point secPoint = new Point(firstPoint.x + middleToLeftRightPointXDis, paddingSize + marginTop3);
        Point thirdPoint = new Point(secPoint.x, halfHeight - marginTop3 + marginTop1);
        Point fourthPoint = new Point(firstPoint.x, halfHeight);
        Point fifthPoint = new Point(paddingSize, halfHeight - marginTop2 + marginTop1);
        Point sixthPoint = new Point(fifthPoint.x, paddingSize + marginTop2);

        Path p = new Path();
        p.moveTo(firstPoint.x, firstPoint.y);
        p.lineTo(secPoint.x, secPoint.y);
        p.lineTo(thirdPoint.x, thirdPoint.y);
        p.lineTo(fourthPoint.x, fourthPoint.y);
        p.lineTo(fifthPoint.x, fifthPoint.y);
        p.lineTo(sixthPoint.x, sixthPoint.y);
        p.close();

        if (drawBooleans[3])
            paint.setStyle(Paint.Style.FILL);
        else
            paint.setStyle(Paint.Style.STROKE);

        canvas.drawPath(p, paint);
    }

    private void drawTopRight(Canvas canvas) {
        if (visilitys[4] == false)
            return;

        int w = getWidth();
        int h = getHeight();
        int halfHeight = h / 2;
        final float factorX = w * 1.0f / niceWidth;
        final float factorY = h * 1.0f / niceHeight;

        final int leftRightToMiddlePointXDis = (int) (6 * factorX);
        final int middleToLeftRightPointXDis = (int) (8 * factorX);
        final int marginTop1 = (int) (6 * factorY);
        final int marginTop2 = (int) (12 * factorY);
        final int marginTop3 = (int) (14 * factorY);

        Point firstPoint = new Point(paddingSize + leftRightToMiddlePointXDis, paddingSize + marginTop1);
        Point secPoint = new Point(firstPoint.x + middleToLeftRightPointXDis, paddingSize + marginTop3);
        Point thirdPoint = new Point(secPoint.x, halfHeight - marginTop3 + marginTop1);
        Point fourthPoint = new Point(firstPoint.x, halfHeight);
        Point fifthPoint = new Point(paddingSize, halfHeight - marginTop2 + marginTop1);
        Point sixthPoint = new Point(fifthPoint.x, paddingSize + marginTop2);

        firstPoint.set(w - firstPoint.x, firstPoint.y);
        secPoint.set(w - secPoint.x, secPoint.y);
        thirdPoint.set(w - thirdPoint.x, thirdPoint.y);
        fourthPoint.set(w - fourthPoint.x, fourthPoint.y);
        fifthPoint.set(w - fifthPoint.x, fifthPoint.y);
        sixthPoint.set(w - sixthPoint.x, sixthPoint.y);

        Path p = new Path();
        p.moveTo(firstPoint.x, firstPoint.y);
        p.lineTo(secPoint.x, secPoint.y);
        p.lineTo(thirdPoint.x, thirdPoint.y);
        p.lineTo(fourthPoint.x, fourthPoint.y);
        p.lineTo(fifthPoint.x, fifthPoint.y);
        p.lineTo(sixthPoint.x, sixthPoint.y);
        p.close();

        if (drawBooleans[4])
            paint.setStyle(Paint.Style.FILL);
        else
            paint.setStyle(Paint.Style.STROKE);

        canvas.drawPath(p, paint);
    }

    private void drawBottomLeft(Canvas canvas) {
        if (visilitys[5] == false)
            return;

        int w = getWidth();
        int h = getHeight();
        int halfHeight = h / 2;
        final float factorX = w * 1.0f / niceWidth;
        final float factorY = h * 1.0f / niceHeight;


        final int leftRightToMiddlePointXDis = (int) (6 * factorX);
        final int middleToLeftRightPointXDis = (int) (8 * factorX);
        final int marginTop1 = (int) (6 * factorY);
        final int marginTop2 = (int) (12 * factorY);
        final int marginTop3 = (int) (14 * factorY);
        final int moveOffset = halfHeight - paddingSize - leftRightToMiddlePointXDis;

        Point firstPoint = new Point(paddingSize + leftRightToMiddlePointXDis, paddingSize + marginTop1);
        Point secPoint = new Point(firstPoint.x + middleToLeftRightPointXDis, paddingSize + marginTop3);
        Point thirdPoint = new Point(secPoint.x, halfHeight - marginTop3 + marginTop1);
        Point fourthPoint = new Point(firstPoint.x, halfHeight);
        Point fifthPoint = new Point(paddingSize, halfHeight - marginTop2 + marginTop1);
        Point sixthPoint = new Point(fifthPoint.x, paddingSize + marginTop2);

        firstPoint.set(firstPoint.x, firstPoint.y + moveOffset);
        secPoint.set(secPoint.x, secPoint.y + moveOffset);
        thirdPoint.set(thirdPoint.x, thirdPoint.y + moveOffset);
        fourthPoint.set(fourthPoint.x, fourthPoint.y + moveOffset);
        fifthPoint.set(fifthPoint.x, fifthPoint.y + moveOffset);
        sixthPoint.set(sixthPoint.x, sixthPoint.y + moveOffset);

        Path p = new Path();
        p.moveTo(firstPoint.x, firstPoint.y);
        p.lineTo(secPoint.x, secPoint.y);
        p.lineTo(thirdPoint.x, thirdPoint.y);
        p.lineTo(fourthPoint.x, fourthPoint.y);
        p.lineTo(fifthPoint.x, fifthPoint.y);
        p.lineTo(sixthPoint.x, sixthPoint.y);
        p.close();

        if (drawBooleans[5])
            paint.setStyle(Paint.Style.FILL);
        else
            paint.setStyle(Paint.Style.STROKE);

        canvas.drawPath(p, paint);
    }

    private void drawBottomRight(Canvas canvas) {
        if (visilitys[6] == false)
            return;

        int w = getWidth();
        int h = getHeight();
        int halfHeight = h / 2;
        final float factorX = w * 1.0f / niceWidth;
        final float factorY = h * 1.0f / niceHeight;

        final int leftRightToMiddlePointXDis = (int) (6 * factorX);
        final int middleToLeftRightPointXDis = (int) (8 * factorX);
        final int marginTop1 = (int) (6 * factorY);
        final int marginTop2 = (int) (12 * factorY);
        ;
        final int marginTop3 = (int) (14 * factorY);

        final int moveOffset = halfHeight - paddingSize - leftRightToMiddlePointXDis;

        Point firstPoint = new Point(paddingSize + leftRightToMiddlePointXDis, paddingSize + marginTop1);
        Point secPoint = new Point(firstPoint.x + middleToLeftRightPointXDis, paddingSize + marginTop3);
        Point thirdPoint = new Point(secPoint.x, halfHeight - marginTop3 + marginTop1);
        Point fourthPoint = new Point(firstPoint.x, halfHeight);
        Point fifthPoint = new Point(paddingSize, halfHeight - marginTop2 + marginTop1);
        Point sixthPoint = new Point(fifthPoint.x, paddingSize + marginTop2);

        firstPoint.set(w - firstPoint.x, firstPoint.y);
        secPoint.set(w - secPoint.x, secPoint.y);
        thirdPoint.set(w - thirdPoint.x, thirdPoint.y);
        fourthPoint.set(w - fourthPoint.x, fourthPoint.y);
        fifthPoint.set(w - fifthPoint.x, fifthPoint.y);
        sixthPoint.set(w - sixthPoint.x, sixthPoint.y);

        firstPoint.set(firstPoint.x, firstPoint.y + moveOffset);
        secPoint.set(secPoint.x, secPoint.y + moveOffset);
        thirdPoint.set(thirdPoint.x, thirdPoint.y + moveOffset);
        fourthPoint.set(fourthPoint.x, fourthPoint.y + moveOffset);
        fifthPoint.set(fifthPoint.x, fifthPoint.y + moveOffset);
        sixthPoint.set(sixthPoint.x, sixthPoint.y + moveOffset);

        Path p = new Path();
        p.moveTo(firstPoint.x, firstPoint.y);
        p.lineTo(secPoint.x, secPoint.y);
        p.lineTo(thirdPoint.x, thirdPoint.y);
        p.lineTo(fourthPoint.x, fourthPoint.y);
        p.lineTo(fifthPoint.x, fifthPoint.y);
        p.lineTo(sixthPoint.x, sixthPoint.y);
        p.close();

        if (drawBooleans[6])
            paint.setStyle(Paint.Style.FILL);
        else
            paint.setStyle(Paint.Style.STROKE);

        canvas.drawPath(p, paint);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        drawTop(canvas);
        drawMiddle(canvas);
        drawBottom(canvas);
        drawTopLeft(canvas);
        drawTopRight(canvas);
        drawBottomLeft(canvas);
        drawBottomRight(canvas);
        super.onDraw(canvas);

    }
}
